<?php

namespace Home\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Home\Model\Menu;
use Home\Model\Tintuc;
use Home\Form\HomeForm;
use Zend\Db\Sql\Select;
use Zend\Paginator\Paginator;
use Zend\Paginator\Adapter\Iterator as paginatorIterator;
use Zend\Session\Container;

class HomeController extends AbstractActionController {

    
	public $menuTable;
	public $tintucTable;
	public $sliderTable;
	public $onlineTable;
	public $lienketTable;
	public $counterTable;
	public $visitTable;
	public $thongtinTable;
	
    public function counter(){
		$sid = session_id();
		$CurrentTime = time();
		$TimeOut = $CurrentTime - 300;
		$this->getOnlineTable()->check_exists($sid,$CurrentTime,$TimeOut);
		$select = new Select();
		$online = $this->getOnlineTable()->fetchAll()->count();
		$this->getEvent()->getViewModel()->online = $online;
		////
		$today = date('d');
		$thang = date('m');
		$nam = date('Y');
		$ip = $this->getRequest()->getServer('REMOTE_ADDR');
		if($this->getVisitTable()->check_date($today,$thang,$nam))
		{
			if($this->getVisitTable()->check_ip($ip,$today,$thang,$nam))
			{
				if($sid!=$this->getVisitTable()->check_ip($ip,$today,$thang,$nam)->session)
				{
					$this->getVisitTable()->updateVisit($ip,$sid,$today,$thang,$nam);
					$this->getCounterTable()->updateLuotxem($today,$thang,$nam);
				}
			}
			else
			{
				$this->getVisitTable()->insertVisit($ip,$sid,$today,$thang,$nam);
				$this->getCounterTable()->updateLuongtruycap($today,$thang,$nam);
			}
		}
		else
		{
			$this->getVisitTable()->deleteVisit();
			$this->getVisitTable()->insertVisit($ip,$sid,$today,$thang,$nam);
			$this->getCounterTable()->insertCounter($today,$thang,$nam);
		}
		//$this->getCounterTable()->check_exists($today,$thang,$nam);
		
		$select = new Select();
		$tongtruycap = $this->getCounterTable()->sumCounter();
		
		$this->getEvent()->getViewModel()->tongtruycap = $tongtruycap->luongtruycap;
		/////// \Zend\Debug\Debug::dump($tongtruycap->luongtruycap);
		
		$select = new Select();
		$thongtin = $this->getThongtinTable()->fetchAll();
		
		$this->getEvent()->getViewModel()->banner = $thongtin->banner;
		$this->getEvent()->getViewModel()->footer = $thongtin->footer;
		$this->getEvent()->getViewModel()->yahoo = $thongtin->yahoo;
		$this->getEvent()->getViewModel()->skype = $thongtin->skype;
	}
	public function indexAction() {
		$module = "home";
		$controller = $this->params('controller');
		$action = $this->params('action');
		
		$sessionModule = new Container('module');
		$sessionController = new Container('controller');
		$sessionAction = new Container('action');
		$sessionModule->lastModule = $module;
		$sessionController->lastController = $controller;
		$sessionAction->lastAction = $action;
		
		
		$select = new Select();
		
		//// domain
		$domain = "/truonghoc";
		$this->getEvent()->getViewModel()->domain = $domain;
		///////
		
        $danhmuc = $this->getMenuTable()->fetchAllDanhmuc();
        $this->getEvent()->getViewModel()->danhmuc = $danhmuc;

		$canbiet = $this->getMenuTable()->fetchAllCanbiet();
        $this->getEvent()->getViewModel()->canbiet = $canbiet;
		
		$menuchinh = $this->getMenuTable()->fetchAllMenuchinh();
        $this->getEvent()->getViewModel()->menuchinh = $menuchinh;
		$this->getEvent()->getViewModel()->menuchinh1 = $menuchinh;
		
		$chuyenmuc = $this->params()->fromRoute('menu');
		$this->getEvent()->getViewModel()->chuyenmuc = $chuyenmuc;
		if($chuyenmuc!=""){
			$select = new Select();
			$tenchuyenmuc = $this->getMenuTable()->getTenchuyenmuc($chuyenmuc)->tenchuyenmuc;
		}
		else{$tenchuyenmuc="";}
		
		$link_bai_viet = $this->params()->fromRoute('id');
		$baiviet = $this->getTintucTable()->getBaiviet($link_bai_viet);
		$baiviet_lienquan = $this->getTintucTable()->getBaivietlienquan($link_bai_viet,$chuyenmuc);
		
		//////////
		$request = $this->getRequest();
		if ($request->isPost()) {
			$order_by = $request->getPost('order_by');
			$order = $request->getPost('order');
			$page = $request->getPost('page');
		}
		else{
			$order_by = $this->params()->fromRoute('order_by') ?
                $this->params()->fromRoute('order_by') : 'id';
			$order = $this->params()->fromRoute('order') ?
                $this->params()->fromRoute('order') : Select::ORDER_ASCENDING;
        	$page = $this->params()->fromRoute('page') ? (int) $this->params()->fromRoute('page') : 1;
		}
        $select = new Select();

        
        

        $tintucs = $this->getTintucTable()->fetchAll($select->where(array('duongdan' => $chuyenmuc,'trangthai' => 'Hiển thị'))->order('id desc'));
        $itemsPerPage = 5;

        $tintucs->current();
        $paginator = new Paginator(new paginatorIterator($tintucs));
        $paginator->setCurrentPageNumber($page)
                ->setItemCountPerPage($itemsPerPage)
                ->setPageRange(7);
		////////
		$select = new Select();
		$sliders = $this->getSliderTable()->fetchAll($select->where(array('trangthai' => 'Hiển thị')));
		
		////
		
		$select = new Select();
		$thongbao = $this->getTintucTable()->fetchAll($select->where(array('duongdan' => 'thong-bao','trangthai' => 'Hiển thị'))->order('id desc')->limit(3));
       ////
	   $select = new Select();
		$tintuc_index = $this->getTintucTable()->fetchAll($select->where(array('trangthai' => 'Hiển thị'))->order('id desc')->limit(10));
		/////
		$select = new Select();
		$noibat = $this->getTintucTable()->fetchAll($select->where(array('tinnoibat' => 'Nổi bật','trangthai' => 'Hiển thị'))->order('id desc')->limit(10));
		$this->getEvent()->getViewModel()->noibat = $noibat;
		
		/////
		
		/////
		$select = new Select();
		$moinhat = $this->getTintucTable()->fetchAll($select->where(array('trangthai' => 'Hiển thị'))->order('id desc')->limit(5));
		$this->getEvent()->getViewModel()->moinhat = $moinhat;
		
		/////
		/////
		$select = new Select();
		$quantam = $this->getTintucTable()->fetchAll($select->where(array('trangthai' => 'Hiển thị'))->order('soluotxem desc')->limit(5));
		$this->getEvent()->getViewModel()->quantam = $quantam;
		
		/////
		
		$select = new Select();
		$lienketwebsite = $this->getLienketTable()->fetchAll();
		$this->getEvent()->getViewModel()->lienketwebsite = $lienketwebsite;
		////
		
		$trangxem = $this->params()->fromRoute('id');
		$luotxem = $this->getTintucTable()->getView($trangxem);
		
		///
		$this->counter();
		
		
        return new ViewModel(array(
					'baiviet_lienquan' => $baiviet_lienquan,
					'baiviet' => $baiviet,
					'chuyenmuc' => $chuyenmuc,
					'tenchuyenmuc' => $tenchuyenmuc,
                    'order_by' => $order_by,
                    'order' => $order,
                    'page' => $page,
                    'paginator' => $paginator,
					'sliders' => $sliders,
					'thongbao' => $thongbao,
					'tintucs' => $tintuc_index,
					
                ));
				
    }

    

    
	public function getMenuTable() {
        if (!$this->menuTable) {
            $sm = $this->getServiceLocator();
            $this->menuTable = $sm->get('Home\Model\MenuTable');
        }
        return $this->menuTable;
    }
	public function getTintucTable() {
        if (!$this->tintucTable) {
            $sm = $this->getServiceLocator();
            $this->tintucTable = $sm->get('Home\Model\TintucTable');
        }
        return $this->tintucTable;
    }
	public function getSliderTable() {
        if (!$this->sliderTable) {
            $sm = $this->getServiceLocator();
            $this->sliderTable = $sm->get('Home\Model\SliderTable');
        }
        return $this->sliderTable;
    }
	public function getOnlineTable() {
        if (!$this->onlineTable) {
            $sm = $this->getServiceLocator();
            $this->onlineTable = $sm->get('Home\Model\OnlineTable');
        }
        return $this->onlineTable;
    }
	public function getLienketTable() {
        if (!$this->lienketTable) {
            $sm = $this->getServiceLocator();
            $this->lienketTable = $sm->get('Home\Model\LienketTable');
        }
        return $this->lienketTable;
    }
	public function getCounterTable() {
        if (!$this->counterTable) {
            $sm = $this->getServiceLocator();
            $this->counterTable = $sm->get('Home\Model\CounterTable');
        }
        return $this->counterTable;
    }
	public function getVisitTable() {
        if (!$this->visitTable) {
            $sm = $this->getServiceLocator();
            $this->visitTable = $sm->get('Home\Model\VisitTable');
        }
        return $this->visitTable;
    }
	public function getThongtinTable() {
        if (!$this->thongtinTable) {
            $sm = $this->getServiceLocator();
            $this->thongtinTable = $sm->get('Home\Model\ThongtinTable');
        }
        return $this->thongtinTable;
    }

}
